<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace addons\video\user\controller;


/**
 * Description of VideoManage
 * 视频分类
 * @author zmh
 */
class VideoClass extends \web\user\controller\AddonUserBase{

    public function index(){
        return $this->fetch();
    }

    /**
     * 列表
     * @return array
     */
    public function loadList(){
        $filter = ' 1=1 ';
        $type = $this->_get('type');
        $show = $this->_get('show');
        if($type!=''){
            $type = $type==1?1:0;
            $filter .= ' and video_type = '.$type;
        }
        if($show!=''){
            $show = $show==1?1:0;
            $filter .= ' and is_show = '.$show;
        }
        $order = 'order_index desc';
        $m = new \addons\video\model\VideoClass();
        $total = $m->getTotal($filter);
        $rows = $m->getDataList($this->getPageIndex(),$this->getPageSize(), $filter,'*',$order);
        return $this->toDataGrid($total, $rows);
    }

    
    public function loadData(){
        $id = $this->_get('id');
        $m = new \addons\video\model\VideoClass();
        $data = $m->getDetail($id);
        return $data;
    }

    public function edit(){
        $m = new \addons\video\model\VideoClass();
        if(IS_POST){
            $data = $_POST;
            $data['update_time'] = NOW_DATETIME;
            if($data['id']){
                $m->save($data);
                return $this->successData();
            }else{
                $data['update_time'] = NOW_DATETIME;
                $m->add($data);
                return $this->successData();
            }
        }else{
            $this->assign('id', $this->_get('id'));
            $this->setLoadDataAction('loadData');
            return $this->fetch();
        }
    }


    public function del(){
        $id = intval($this->_post('id'));
        if (!empty($id)) {
            $m = new \addons\video\model\VideoClass();
            try {
                $res = $m->deleteData($id);
                if ($res > 0) {
                    return $this->successData();
                }
            } catch (\Exception $e) {
                return $this->failData($e->getMessage());
            }
        } else {
            return $this->failData('删除失败，参数有误');
        }
    }
}
